<?php
namespace app\index\controller;

use think\Controller;
use think\Response\Json;
use think\Db;

/**
* 前台首页
*/
class Pages extends Base
{
	public function _initialize() {
		parent::_initialize();

        $request                  = request();
        $params                   = $request->param();
        $cat_id                   = $request->has('cat_id');
        $id                       = $request->has('id');
        $k                        = $request->has('k');

        $this->params             = $params;
        $this->request            = $request;

        switch (get_action()) {
            case 'detail':
                if(!$id) {
                    $this->redirect('err');
                } else {
                    $this->detail($params['id']);
                }
                break;
            case 'news':
                if(!$cat_id) {
                    $this->redirect('err');
                } else {
                    $this->news($params['cat_id']);
                }
                break;
            case 'work':
                if(!$cat_id) {
                    $this->redirect('err');
                } else {
                    $this->work($params['cat_id']);
                }
                break;
            case 'form':
                break;
            case 'work2':
                if(!$cat_id) {
                    $this->redirect('err');
                }
                $this->work2($params['cat_id']);
                break;
            case 'search':
                if(!$k) {
                    $this->redirect('err');
                } else {
                    $this->search($params['k']);
                }
                break;
            case 'work1':
                $this->work1();
                break;
            case 'progress':
                if(!$id) {
                    $this->redirect('err');
                } else {
                    $this->progress($params['id']);
                }
                break;
            case 'lists':
                $this->lists();
                break;
            case 'notice':
                $this->notice();
                break;
            default:
                # code...
                break;
        }
	}

    public function news($cat_id)
    {
        $model1                = Db::name('category');
        $model2                = Db::name('content');
        $map['id']             = $cat_id;
        $data                  = Db::name('category')->where($map)->find();
        $posts                 = $this->get_posts($cat_id); 

        $this->assign('posts', $posts);
        $this->assign('cat_desc', $data['desc']);
       

    	return $this->fetch();
    }

    public function detail() {
    	return $this->fetch();
    }

    public function search($k) {
        $params                 = $this->params;
        $request                = $this->request;
        $k                      = $this->inject_check($k);
        $map['title']           = array('like', "%".$k."%");
        $p                      = $request->has('p') ? $params['p'] : 1;
        $limit                  = $request->has('limit') ? $params['limit'] : 8;
        $count                  = Db::name('content')->where($map)->count();
        $posts                  = Db::name('content')->where($map)->limit($p - 1, $limit)->select();

        $this->assign('p', $p);
        $this->assign('limit', $limit);
        $this->assign('count', $count);
        $this->assign('ap', ceil($count / $limit));
        $this->assign('posts', $posts);
        return $this->fetch();  
    }

    public function err() {
        $this->assign('title', 'This is my 404');
        return $this->fetch('Pages/err');
    }

    public function work($cat_id) {
        $model1                   = Db::name('content');
        $map['is_recommend']      = 1;
        $map['cid']               = $cat_id;
        $recommend_p              = $model1->where($map)->find();
        if($recommend_p){
            $recommend_p['time']    = date('Y-m-d', $recommend_p['time']);
        }
        $map['is_recommend']      = 0;
        $posts                    = $this->get_posts($cat_id, $map);
        $model1->where($map)->select();
        $this->assign('recommend_p', $recommend_p);
        $this->assign('posts', $posts);
        return $this->fetch('Pages/work');
    }

    public function work1() {
        $JOB                = ['学生', '电器类', '木工', '电子信息类', '玻璃维修'];
        $map['post']        = array(array('neq', 0), array('neq', 999));
        $list               = Db::name('job a')->join('web_user b', 'a.uid=b.uid')->field('a.*, b.mobile, b.headimgurl, b.nickname, b.truename, b.email, b.sex')->where($map)->select();
        $lists              = array();


        foreach ($list as $key => $value) {
              $name           = $JOB[$value['post']];
              if(empty($lists[$name])) {
                $lists[$name]  = array();
              }
              $lists[$name][]  = $value; 
        }  

        $this->assign('lists', $lists);

        return $this->fetch('Pages/work1');
    }

    public function progress($id = null) {
        $JOB                = ['学生', '电器类', '木工', '电子信息类', '玻璃维修'];
        $map['id']          = $id;
        $map1['repair_id']  = $id;
        $detail             = Db::name('repair')->where($map)->find();
        $detail['time']     = date('Y-m-d H:i:s', $detail['time']);
        $progress           = Db::name('progress')->where($map1)->order('status desc')->select();
        $comment            = Db::name('comment')->where($map1)->find();

        if($comment) {
            $comment['time']    = date('Y-m-d', $comment['time']);
            $comment['to']      = Db::name('user')->where(array('uid' => $comment['to']))->field('headimgurl, uid, truename, username, sex, address')->find();
            $comment['from']      = Db::name('user')->where(array('uid' => $comment['from']))->field('headimgurl, uid, truename, username, sex, address')->find();
        }

        foreach ($progress as $key => &$value) {
            $time           = date('Y-m-d H:i:s', $value['time']);
            $value['time']  = $time;
            $value['date']  = explode(' ', $time)[0];
            $value['times'] = explode(' ', $time)[1];
        }

        $this->assign('comment', $comment);
        $this->assign('detail', $detail);
        $this->assign('progress', $progress);
        return $this->fetch('Pages/progress');
    }

    public function lists() {
        return $this->fetch('Pages/lists');
    }

    public function notice() {
        // 跳转外链
        $this->redirect('http://www.mmvtc.cn/templet/default/ShowClassPage.jsp?id=914');
    }


    public function form() {
        return $this->fetch('Pages/form');
    }

    public function work2($cat_id) {
        return $this->fetch('Pages/work2');
    }

    public function get_posts($cat_id, $map = []) {
        $model2                   = Db::name('content');
        $request                  = request();
        $params                   = $request->param();

        $map['cid']               = $cat_id;

        $p                        = $request->has('p') ? $params['p'] : 1;
        $limit                    = $request->has('limit') ? $params['limit'] : 8;
        $count                    = $model2->where($map)->count();


        $this->assign('p', $p);
        $this->assign('limit', $limit);
        $this->assign('count', $count);
        $this->assign('ap', ceil($count / $limit));

        return $model2->where($map)->limit($p-1, $limit)->select();
    }

    // 格式化日期
    public function parse_date($data, $parse="Y-m-d H:i") {
      foreach ($data as $key => &$value) {
        if($value['cTime']) {
          $value['cTime'] = date($parse, $value['cTime']);
        }

        if($value['post_date']) {
            $value['post_date'] = date($parse, $value['post_date']);
        }

        if($value['post_modified']) {
            $value['post_modified'] = date($parse, $value['post_modified']);
        }

      }
      return $data;
    }




    public function inject_check($sql_str) {
       $check = preg_match('/select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile/', $sql_str);

       if($check)
       {
        echo "非法字符!";
         exit();
       }else
       {
         return $sql_str;
       }
    }

}
